package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.StockDao;
import cn.gson.crm.model.dao.WaouDetailsDao;
import cn.gson.crm.model.dao.WarehousingDao;
import cn.gson.crm.model.mapper.RepairOrderMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.mapper.WarehousingMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class WarehousingService {
    @Autowired
    WarehousingDao warehousingDao;
    @Autowired
    WaouDetailsDao waouDetailsDao;
    @Autowired
    WarehousingMapper warehousingMapper;
    @Autowired
    StockDao stockDao;
    @Autowired
    RepairOrderMapper repairOrderMapper;
    @Autowired
    StocksService stocksService;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //批量新增采购入库单
    public void piliansave(List<Purchase> purchaseList){
        List<WaouDetails> waouDetails=null;
        List<StockDetails> stockDetails=new ArrayList<>();
        Stock stock=new Stock();
        stock.setKcName("主仓库");
        Timestamp d = new Timestamp(System.currentTimeMillis());
        Employee e=new Employee();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        for(int i=0;i<purchaseList.size();i++){
            repairOrderMapper.updatecaig((int) purchaseList.get(i).getPurchaseId());
            Warehousing warehousing=new Warehousing();
            e.setYgId(1);
            warehousing.setEmployee(e);
            warehousing.setWaDanhao("RK"+newDate+result);
            warehousing.setWaTime(d);
            warehousing.setWaShuoming("采购入库");
            warehousing.setWaState("已入库");
            warehousing.setThdanhao(purchaseList.get(i).getPurchaseDh());
            List<Purchasexq> purchasexqList=repairOrderMapper.selectcaixiang((int) purchaseList.get(i).getPurchaseId());
            for(int c=0;c<purchasexqList.size();c++){
                WaouDetails waouDetails1=new WaouDetails();
                StockDetails stockDetails1=new StockDetails();
                waouDetails1.setWoCaijia(purchasexqList.get(c).getPurchasexqDj());
                waouDetails1.setWoColor(purchasexqList.get(c).getPurchasexqYs());
                waouDetails1.setWoCompany(purchasexqList.get(c).getPurchasexqDw());
                waouDetails1.setWoName(purchasexqList.get(c).getPurchaseName());
                waouDetails1.setWoPrice(purchasexqList.get(c).getPurchasexqDj());
                waouDetails1.setWoSize(purchasexqList.get(c).getPurchasexqCc());
                waouDetails1.setWoNumber(purchasexqList.get(c).getPurchasexqNumber());
                waouDetails1.setWarehousing(warehousing);
                waouDetails.add(waouDetails1);
                stockDetails1.setsCaijia(purchasexqList.get(c).getPurchasexqDj());
                stockDetails1.setsCompany(purchasexqList.get(c).getPurchasexqDw());
                stockDetails1.setsSize(purchasexqList.get(c).getPurchasexqCc());
                stockDetails1.setsName(purchasexqList.get(c).getPurchaseName());
                stockDetails1.setsPrice(purchasexqList.get(c).getPurchasexqDj());
                stockDetails1.setsNumber(purchasexqList.get(c).getPurchasexqNumber());
                stockDetails1.setsColor(purchasexqList.get(c).getPurchasexqYs());
                stockDetails.add(stockDetails1);
            }
            stock.setStockDetailsList(stockDetails);
            warehousingDao.save(warehousing);
            waouDetailsDao.saveAll(waouDetails);
            stocksService.savestocl(stock);
        }
    }
    //批量新增维修入库单
    public void piliansaveweixu(List<RepairOrder> repairOrder){
        List<WaouDetails> waouDetails=new ArrayList<>();
        Stock stock=new Stock();
        List<StockDetails> stockDetails=new ArrayList<>();
        stock.setKcName("主仓库");
        Employee e=new Employee();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Timestamp d = new Timestamp(System.currentTimeMillis());
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        for(int i=0;i<repairOrder.size();i++){
            repairOrderMapper.updateweixx("已入库",repairOrder.get(i).getRepairId());
            Warehousing warehousing=new Warehousing();
            e.setYgId(1);
            warehousing.setEmployee(e);

            warehousing.setWaDanhao("RK"+newDate+result);
            warehousing.setWaTime(d);
            warehousing.setWaShuoming("维修入库");
            warehousing.setWaState("已入库");
            warehousing.setThdanhao(repairOrder.get(i).getRepairNumber());
            List<RepairOrderDetails> repairOrderDetails=repairOrderMapper.selectxian(repairOrder.get(i).getRepairId());
            for(int c=0;c<repairOrderDetails.size();c++){
                WaouDetails waouDetails1=new WaouDetails();
                StockDetails stockDetails1=new StockDetails();
                waouDetails1.setWoColor(repairOrderDetails.get(c).getRodColor());
                waouDetails1.setWoCompany(repairOrderDetails.get(c).getRodUompany());
                waouDetails1.setWoName(repairOrderDetails.get(c).getRodName());
                waouDetails1.setWoPrice(repairOrderDetails.get(c).getRodUnitprice());
                waouDetails1.setWoSize(repairOrderDetails.get(c).getRodSize());
                waouDetails1.setWoNumber( repairOrderDetails.get(c).getRodNumber());
                waouDetails1.setWarehousing(warehousing);
                waouDetails.add(waouDetails1);
                stockDetails1.setsCompany(repairOrderDetails.get(c).getRodUompany());
                stockDetails1.setsSize(repairOrderDetails.get(c).getRodSize());
                stockDetails1.setsName(repairOrderDetails.get(c).getRodName());
                stockDetails1.setsPrice(repairOrderDetails.get(c).getRodUnitprice());
                stockDetails1.setsNumber(repairOrderDetails.get(c).getRodNumber());
                stockDetails1.setsColor(repairOrderDetails.get(c).getRodColor());
                stockDetails.add(stockDetails1);
            }
            stock.setStockDetailsList(stockDetails);
            warehousingDao.save(warehousing);
            waouDetailsDao.saveAll(waouDetails);
            stocksService.savestocl(stock);
        }
    }
    //新增
    public void save(Warehousing warehousing){
        warehousingDao.save(warehousing);
        warehousing.getWaouDetailsList().forEach(p->{
            p.setWarehousing(warehousing);
        });
        waouDetailsDao.saveAll(warehousing.getWaouDetailsList());
    }

    //查询当前登录人入库记录
    public Map<String,Object> selectwar(HttpSession session,Integer yid, Integer page, Integer size){
        yid=fouyuan(session);
        List<Warehousing> warehousings=warehousingMapper.selectwar(yid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Warehousing> warehousings1=warehousingMapper.selectwar(yid);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",warehousings1);
        map.put("tong",warehousings);
        return map;

    };
    //时间查询
    public Map<String,Object> selecttime(HttpSession session,Integer yid,String statr, String endtime, Integer page, Integer size){
        yid=fouyuan(session);
        List<Warehousing> outbounds2=warehousingMapper.selecttime(yid,statr,endtime);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Warehousing> outbounds=warehousingMapper.selecttime(yid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",outbounds);
        map.put("tong",outbounds2);
        return map;
    };
    //条件查询
    public Map<String,Object> selectwhere(HttpSession session,Integer yid, String where,Integer page,Integer size){
        yid=fouyuan(session);
        List<Warehousing> outbounds2=warehousingMapper.selectwhere(yid,where);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Warehousing> outbounds=warehousingMapper.selectwhere(yid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",outbounds);
        map.put("tong",outbounds2);
        return map;
    };
    //修改入库状态
    public void updateruku(Integer wid){
        warehousingMapper.updateruku(wid);
    };
    //查详情
    public List<WaouDetails> selectwaDel(Integer wid){
        return warehousingMapper.selectwaDel(wid);
    };

}
